{% extends "base.html" %}
{% block title %}Vnos rezultatov pisnega dela{% endblock %}

{% block js %}
<script type="text/javascript">
{% if exam %}
  function checkPoints(el){
    value = $(el).val();
    if (value.length > 0){
      if (is_int(value)){
        value = parseInt(value);
        if (value < 0) {
          alert("Ni veljavno število točk!");
          $(el).addClass("error");
          return false;
        }else {
          if ($(el).hasClass("input_points_rest")){
            if (value > {{ exam.max_points_rest }}){
              alert("Ni veljavno število točk! Maksimalno {{ exam.max_points_rest }}.");
              $(el).addClass("error");
              return false;
            } else {
              $(el).removeClass("error");
            }
          }else if ($(el).hasClass("input_points_e")){
            if (value > {{ exam.max_points }}){
              alert("Ni veljavno število točk! Maksimalno {{ exam.max_points }}.");
              $(el).addClass("error");
              return false;
            } else {
              $(el).removeClass("error");
            }            
          }
          
        }
      }else {
        alert("Ni veljavno število točk!");
        $(el).addClass("error");
        return false;
      }
    }else {
      $(this).removeClass("error");
    }   
    return true;
  }
{% endif %}
function is_int(value){
  if((parseFloat(value) == parseInt(value)) && !isNaN(value)){
      return true;
  } else {
      return false;
  }
}

function checkSubmit(){
  {% if exam %}
  $(".input_points").each(function(index, el){
    checkPoints(el);
  });
  {% endif %}
  s = $(".error").size();
  if (s > 0){
    alert("Odpravi napake pred shranjevanjem.");
    return false;
  }else {
    return true;
  }
}


$(document).ready(function(){

  $("#id_exam").change(function(){
    selected_value = $('#id_exam option:selected').val();
    if (selected_value != undefined && selected_value != "") {
      document.location="/exams/wgrade/{{ professor.pk }}/"+selected_value+"/";
    }
  });
  
  
  $(".input_vp").change(function(){
  
    var checked = $(this).is(':checked');
    $(".input_points",$(this).parent().parent()).each(function(index,el){
    
      if (checked) {
        $(this).attr('disabled', 'disabled');
      }else {
        $(this).removeAttr('disabled');
      }
    
    });
    
  });
    
    

  
  $(".input_points").change(function(){
    checkPoints(this);
  });
  
  
  
});



</script>
{% endblock %}

{% block content %}

{% if only_search %}
<script type="text/javascript" src="{{ MEDIA_URL }}js/search.js"></script>
<script type="text/javascript">

$(document).ready(function(){    
    search_setcb('professor',function(professor){
      url = '/exams/wgrade/'+professor.pk+'/';
      document.location = url;
    });
    
});
</script>
{% include "snippets/professor/search.htm" %}
{% else %}
<p>
  <b>Izbrani profesor: </b> {{ professor }}<br />
  <a href="/exams/wgrade/">Ponovna izbira profesorja</a>
</p><br />
<form method="post">
{% csrf_token %}
	{{ form.as_p }}
</form>
{% if data != None %}

<h2>Seznam študentov</h2>
<p><b>Seznam za izpit:</b> {{ exam }} <br />
   <b>Izvajalec:</b> {{ exam.show_professor }}
</p>
{% if saved %}
<div class="info_msg">Podatki so bili shranjeni!</div>
{% endif %}
<form method="POST" onSubmit="javascript: return checkSubmit();">
{% csrf_token %}
<table class="list">
  <tr>
    <th class="num">#</th>
    <th>Vpisna številka</th>
    <th>Priimek ime</th>
    <th>Št. polaganj</th>
    <th>Študijsko leto</th>
    <th>Št. točk</th>
    <th>Točke ostalo</th>
    <th>VP</th>
  </tr>
  {% for a in data %}
  <tr>
    <td>{{ forloop.counter }}</td>
    <td>{{ a.student.enrollment_number }}</td>
    <td>{{ a.student.user.last_name }} {{ a.student.user.first_name }}</td>
    <td>{{ a.num_app }}</td>
    <td>{{ a.exam.subject.school_year_start }} / {{ a.exam.subject.school_year_end }}</td>
    <td><input class="input_points input_points_e" {% if a.exam_grade == 0 or a.practice_grade == 0 %}disabled="disabled"{% endif %} type="text" value="{% if a.points %}{{ a.points }}{% endif %}" name="application.{{ a.id }}.points" /></td>
    <td><input class="input_points input_points_rest" {% if a.exam_grade == 0 or a.practice_grade == 0 %}disabled="disabled"{% endif %} type="text" value="{% if a.points_rest %}{{ a.points_rest }}{% endif %}" name="application.{{ a.id }}.points_rest" /></td>
    <td><input class="input_vp" {% if a.exam_grade == 0 or a.practice_grade == 0 %}checked="checked"{% endif %} type="checkbox" name="application.{{ a.id }}.vp" value="1" /></td>
  </tr>
  {% endfor %}
</table>
{% if data.count > 0 %}
<input type="submit" value="Shrani" />
{% endif %}
</form>
{% endif %}

{% endif %}
{% endblock %}